/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package applet.visualisation;

import java.io.*;
import org.jdom.*;

/**
 *
 * @author krishen
 */
public class GenNodes {public static void main(String[] args) throws FileNotFoundException, IOException {
        
        String[] keyGraphNode = {"name", "gender"};

        int n =7000;

        File ff = new File("/home/krishen/allNodesTest.xml");

        Element graphml = new Element("graphml");
        Namespace nsGraphml = Namespace.getNamespace("http://graphml.graphdrawing.org/xmlns");
        graphml.setNamespace(nsGraphml);


        Element graph = new Element("graph");
        Attribute edgedefault = new Attribute("edgedefault", "undirected");
        graph.setAttribute(edgedefault);

        for (int i = 0; i < keyGraphNode.length; i++) {
                graph.addContent(getKeyGraph(i, keyGraphNode, "node"));

            }

        Element nodes = new Element("nodes");
        org.jdom.Document document = new org.jdom.Document(graphml);

        for (int i = 0; i<n; i++){
            Element node = new Element("node");
            Attribute id = new Attribute("id", String.valueOf(i));
            node.setAttribute(id);
            
            
            Element name = new Element("data");
            Attribute nameAt = new Attribute("key", "name");
            name.setAttribute(nameAt);
            name.setText("toto"+i);
            node.addContent(name);

            Element gender = new Element("data");
            Attribute genderAt = new Attribute("key", "gender");
            gender.setAttribute(genderAt);
            gender.setText("M");
            node.addContent(gender);

            nodes.addContent(node);
        }

        // <edge source="1" target="2"></edge>
        for (int i=0;i<n; i++){
            Element edge = new Element("edge");

            Attribute source = new Attribute("source", String.valueOf(i));
            Attribute target = null;
            if (i == (n-1)){
                target = new Attribute("target", String.valueOf(i-1));
            }else{
                target = new Attribute("target", String.valueOf(i+1));
            }
            

            edge.setAttribute(source);
            edge.setAttribute(target);

            nodes.addContent(edge);

        }
        graph.addContent(nodes);
        graphml.addContent(graph);
        
        

        org.jdom.output.XMLOutputter sortie = new org.jdom.output.XMLOutputter(org.jdom.output.Format.getPrettyFormat());
        sortie.output(document, new FileOutputStream(ff.getAbsolutePath()));
        
    }

    private static Element getKeyGraph(int i, String[] tableauKey, String type) {

        Element key = new Element("key");
        Attribute keyId = new Attribute("id", tableauKey[i]);
        key.setAttribute(keyId);

        Attribute keyFor = new Attribute("for", type);
        key.setAttribute(keyFor);

        Attribute keyAName = new Attribute("attr.name", tableauKey[i]);
        key.setAttribute(keyAName);

        Attribute keyType = new Attribute("attr.type", "string");
        key.setAttribute(keyType);

        return key;
    }




}
